package cn.com.assistant.service.impl;

import cn.com.assistant.entity.AnswerEntity;
import cn.com.assistant.entity.DataEntity;
import cn.com.assistant.model.ImageDTO;
import cn.com.assistant.mapper.AnswerMapper;
import cn.com.assistant.mapper.CasAnswerMapper;
import cn.com.assistant.mapper.DataMapper;
import cn.com.assistant.service.AnswerService;
import cn.com.assistant.utils.ResponseMessage;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class AnswerServiceImpl extends ServiceImpl<AnswerMapper, AnswerEntity> implements AnswerService {

    @Autowired
    AnswerMapper answerMapper;

    @Autowired
    CasAnswerMapper casAnswerMapper;

    @Autowired
    DataMapper dataMapper;


    @Override
    public Boolean deleteByQuestionId(String questionId) {
        // 查询所有一级回复的id
        List<String> ids = answerMapper.findIdByQId(questionId);
        if (CollectionUtils.isNotEmpty(ids)) {
            // 删除二级回复
            casAnswerMapper.deleteByPId(ids);
        }
        // 删除一级回复
        answerMapper.deleteByQId(questionId);
        return true;
    }

    @Override
    public ResponseMessage findByPage(int pageNum, int size) {
        Page<?> page = new Page<>(pageNum, size);
        IPage<Map<String, Object>> mapIPage = dataMapper.selectPageVo(page);
        return ResponseMessage.defaultResponse().setData(mapIPage);

    }

    @Override
    public ResponseMessage findByQid(String questionId) {
        List<AnswerEntity> answerEntities = answerMapper.findByQId(questionId);
        if (CollectionUtils.isNotEmpty(answerEntities)) {
            for (int i = 0; i < answerEntities.size(); i++) {
                AnswerEntity answerEntity = answerEntities.get(i);
                // 处理多张图片，根据分号处理成list
                List<String> imgUrlList = new ArrayList<>();
                if (StringUtils.isNotEmpty(answerEntity.getImgUrl())) {
                    imgUrlList = Arrays.asList(answerEntity.getImgUrl().split(";"));
                }
                answerEntity.setImgUrlList(imgUrlList);
            }
        }
        return ResponseMessage.defaultResponse().setData(answerEntities);
    }

    @Override
    public ResponseMessage update(AnswerEntity answerEntity) {
        answerEntity.setUpdatedDate(new Date());
        // 处理多张图片，追加分号";"
        List<String> imgUrlList = answerEntity.getImgUrlList();
        if (CollectionUtils.isNotEmpty(imgUrlList)) {
            String imgUrl = imgUrlList.get(0) + ";";
            for (int j = 1; j < imgUrlList.size(); j++) {
                String url = imgUrlList.get(j) + ";";
                imgUrl += url;
            }
            answerEntity.setImgUrl((imgUrl));
        } else {
            answerEntity.setImgUrl((null));
        }
        int num = answerMapper.update(answerEntity);
        if (num == 1) {
            return ResponseMessage.defaultResponse().setMsg("修改成功！");
        } else {
            return ResponseMessage.failResponse().setMsg("修改失败！");
        }
    }

    @Override
    public ResponseMessage updateImage(ImageDTO imageDTO) {
        AnswerEntity answerEntity = new AnswerEntity();
        answerEntity.setUpdatedDate(new Date());
        answerEntity.setId(imageDTO.getId());
        // 处理多张图片，追加分号
        List<String> imgUrlList = imageDTO.getImgUrlList();
        if (CollectionUtils.isNotEmpty(imgUrlList)) {
            String imgUrl = imgUrlList.get(0) + ";";
            for (int j = 1; j < imgUrlList.size(); j++) {
                String url = imgUrlList.get(j) + ";";
                imgUrl += url;
            }
            answerEntity.setImgUrl((imgUrl));
        } else {
            answerEntity.setImgUrl((null));
        }

        int flag = answerMapper.updateImage(answerEntity);
        if (flag == 1) {
            return ResponseMessage.defaultResponse().setMsg("修改成功！");
        } else {
            return ResponseMessage.failResponse().setMsg("修改失败！");
        }
    }

    @Override
    public ResponseMessage getAllFault() {
        // 查询所有故障
        List<DataEntity> dataEntities = dataMapper.findAllData();
        List<Map<String, Object>> allFault = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(dataEntities)) {
            for (int i = 0; i < dataEntities.size(); i++) {
                DataEntity dataEntity = dataEntities.get(i);
                // 存放故障名及解决措施
                Map<String, Object> faultResult = new HashMap<>();
                // 查询一级回复
                List<Map<String, Object>> answerList = answerMapper.findMapByQId(dataEntity.getSolutions());
                if (CollectionUtils.isNotEmpty(answerList)) {
                    for (int j = 0; j < answerList.size(); j++) {
                        Map<String, Object> answerMap = answerList.get(j);
                        if (null != answerMap) {
                            // 查询二级回复
                            List<Map<String, Object>> casAnswerEntities = casAnswerMapper.findMapByPId(String.valueOf(answerMap.get("id")));
                            answerMap.put("casAnswerEntities", casAnswerEntities) ;
                        }
                    }
                }
                faultResult.put("fault", dataEntity.getFaultName());
                faultResult.put("faultPhe", dataEntity.getQuestion());
                faultResult.put("solutions", answerList);
                allFault.add(faultResult);
            }
        }
        return ResponseMessage.defaultResponse().setData(allFault);
    }


}